package com.xuecheng.content.application.event.handler;

import cn.hutool.core.bean.BeanUtil;
import com.xuecheng.content.application.event.CourseBaseUpdateEvent;
import com.xuecheng.content.domain.model.CourseBaseDo;
import com.xuecheng.content.domain.model.entity.CourseBase;
import com.xuecheng.content.domain.model.entity.CourseMarket;
import com.xuecheng.content.domain.service.CourseBaseService;
import com.xuecheng.content.domain.service.CourseMarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-07-28  20:42
 */
@Component
public class CourseBaseUpdateEventHandler {

    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private CourseMarketService courseMarketService;

    @Async
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = CourseBaseUpdateEvent.class, fallbackExecution = true)
    public void CourseBaseUpdate(CourseBaseUpdateEvent event){
        // 修改课程基本信息
        CourseBaseDo editCourseDto = event.getCourseBaseDo();
        CourseBase updateCourseBase = BeanUtil.copyProperties(editCourseDto, CourseBase.class);
        courseBaseService.updateById(updateCourseBase);
    }

    @Async
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = CourseBaseUpdateEvent.class, fallbackExecution = true)
    public void CourseMarkUpdate(CourseBaseUpdateEvent event){
        // 修改课程营销信息
        CourseBaseDo editCourseDto = event.getCourseBaseDo();
        CourseMarket updateCourseMark = BeanUtil.copyProperties(editCourseDto, CourseMarket.class);
        courseMarketService.saveOrUpdateCourseMark(updateCourseMark);
    }
}
